package org.rcy.ruicingmarket.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.rcy.framework.api.entity.ResultMessage;
import org.rcy.framework.cache.redis.CommonCache;
import org.rcy.ruicingmarket.cache.OssCacheGroup;
import org.rcy.ruicingmarket.common.utils.TokenUtils;
import org.rcy.ruicingmarket.oss.user.OssUserService;
import org.rcy.ruicingmarket.resource.ApiGroup;
import org.rcy.ruicingmarket.security.PrivilegeCheck;
import org.rcy.ruicingmarket.user.BindRolesVo;
import org.rcy.ruicingmarket.user.OssAddUserVo;
import org.rcy.ruicingmarket.user.OssUserVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 吴康桥
 * @Description
 * @date 2022/9/23 14:00
 */

@RestController
@RequestMapping("/user")
@Tag(name = "ossuser", description = "后台用户控制器")
public class OssUserController {
	protected final Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private OssUserService userService;
	@Value("${oss.token.encryptkey}")
	private String tokenKey;
	@Autowired
	private CommonCache commonCache;

	@PostMapping("/add")
	@Operation(summary = "添加用户", description = "添加用户", tags = {"ossuser"})
	@Parameter(name = "ossInnerToken", required = true , in = ParameterIn.HEADER)
	@PrivilegeCheck(group = ApiGroup.SYSTEM,name = "添加用户",auth = true)
	public ResultMessage addUser(@RequestBody OssAddUserVo vo){
		try {
			userService.add(vo);
			return ResultMessage.success();
		}catch (Exception e) {
			logger.error(e.getMessage(),e);
			return ResultMessage.error();
		}
	}

	@PostMapping("/bindroles")
	@Operation(summary = "绑定角色", description = "绑定角色", tags = {"ossuser"})
	@Parameter(name = "ossInnerToken", required = true , in = ParameterIn.HEADER)
	@PrivilegeCheck(group = ApiGroup.SYSTEM,name = "绑定角色",auth = true)
	public ResultMessage bindRoles(@RequestBody BindRolesVo vo){
		try {
			userService.bindRoles(vo);
			return ResultMessage.success();
		}catch (Exception e){
			return ResultMessage.error();
		}
	}

	@GetMapping("/currentuser")
	@Operation(summary = "查询当前登录用户", description = "查询当前登录用户", tags = {"ossuser"})
	public ResultMessage currUser(HttpServletRequest request){
		try {
			String ossInnerToken = request.getHeader("ossInnerToken");
			if(StringUtils.isBlank(ossInnerToken)){
				return ResultMessage.error();
			}
			Long userId = TokenUtils.getIdFromToken(ossInnerToken, tokenKey);
			return ResultMessage.success(userId);
		}catch (Exception e) {
			return ResultMessage.error();
		}
	}

}
